package cn.edu.qfnu.oj.service.impl;

import cn.edu.qfnu.common.service.Impl.BaseServiceImpl;
import cn.edu.qfnu.oj.dao.ProblemDao;
import cn.edu.qfnu.oj.entity.Problem;
import cn.edu.qfnu.oj.entity.enums.Difficulty;
import cn.edu.qfnu.oj.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * OnlineJudge of QF_NU
 * Created by LS on 2017/7/18.
 */
@Service("problemService")
public class ProblemServiceImpl extends BaseServiceImpl<Problem, Integer> implements ProblemService {

    private final ProblemDao problemDao;

    @Autowired
    public ProblemServiceImpl(ProblemDao problemDao) {
        this.problemDao = problemDao;
    }

    public Difficulty checkDifficulty(String difficulty) {
        Difficulty level;
        switch (difficulty) {
            case "青铜":
                level = Difficulty.BRONZE;
                break;
            case "白银":
                level = Difficulty.SILVER;
                break;
            case "黄金":
                level = Difficulty.GOLD;
                break;
            case "钻石":
                level = Difficulty.DIAMOND;
                break;
            case "王者":
                level = Difficulty.KING;
                break;
            default:
                level = Difficulty.BRONZE;
                break;
        }

        return level;
    }

    public Boolean checkIfACM(String isACM) {
        Boolean acm;

        switch (isACM) {
            case "ACM":
                acm = true;
                break;
            default:
                acm = false;
                break;
        }
        return acm;
    }

    public Boolean checkIfTemp(String isTemp) {
        Boolean temp;
        switch (isTemp) {
            case "临时":
                temp = true;
                break;
            default:
                temp = false;
                break;
        }
        return temp;
    }


    @Override
    public Problem createProblem(String title, String content,
                              String inputDesc, String outputDesc, String sampleInput, String sampleOutput,
                              String classify, LocalDateTime createTime, String difficulty, String hint, String sources,
                              Integer memoryLimit, Integer timeLimit, String isAcm, String isTemp) {


        return new Problem(title, content, inputDesc, outputDesc, sampleInput, sampleOutput,
                classify, createTime, checkDifficulty(difficulty), hint, sources,
                memoryLimit, timeLimit, checkIfACM(isAcm), checkIfTemp(isTemp));

    }

    @Override
    public Problem modify(Problem problem, String title, String content,
                       String inputDesc, String outputDesc, String sampleInput, String sampleOutput,
                       LocalDateTime modifyTime, String classify, String difficulty, String hint, String sources,
                       Integer memoryLimit, Integer timeLimit, String isAcm, String isTemp) {

        //通过DynamicUpdate动态更新信息 以提高效率
        if (!title.equals(problem.getTitle())) {
            problem.setTitle(title);
        }
        if (!content.equals(problem.getContent())) {
            problem.setContent(content);
        }
        if (!inputDesc.equals(problem.getInputDesc())) {
            problem.setInputDesc(inputDesc);
        }
        if (!outputDesc.equals(problem.getOutputDesc())) {
            problem.setOutputDesc(outputDesc);
        }
        if (!sampleInput.equals(problem.getSampleInput())) {
            problem.setSampleInput(sampleInput);
        }
        if (!sampleOutput.equals(problem.getSampleOutput())) {
            problem.setSampleOutput(sampleOutput);
        }
        if (!checkDifficulty(difficulty).equals(problem.getDifficulty())) {
            problem.setDifficulty(checkDifficulty(difficulty));
        }
        if (!classify.equals(problem.getClassify())) {
            problem.setClassify(classify);
        }
        if (!hint.equals(problem.getHint())) {
            problem.setHint(hint);
        }
        if (!sources.equals(problem.getSources())) {
            problem.setSources(sources);
        }
        if (!memoryLimit.equals(problem.getMemoryLimit())) {
            problem.setMemoryLimit(memoryLimit);
        }
        if (!timeLimit.equals(problem.getTimeLimit())) {
            problem.setTimeLimit(timeLimit);
        }
        if (!checkIfACM(isAcm).equals(problem.getACM())) {
            problem.setACM(checkIfACM(isAcm));
        }
        if (!checkIfTemp(isTemp).equals(problem.getTemp())) {
            problem.setTemp(checkIfTemp(isTemp));
        }
        problem.setModifyTime(modifyTime);

        return problem;
    }
}
